<?php

/**
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class Nina_Timestamp
{
	/**
	 * Get the current timestamp
	 * @return integer
	 */
	public static function now() {
		return date("YmdHis");
	}
	
	/**
	 * Convert a date to a NINA timestamp
	 * @param Date $data The date to convert
	 * @return integer
	 */
	public static function toNinaDate($date = false) {
		if(!$date) $date = time();
		return date("YmdHis", $date);
	}
	
	/**
	 * Format a NINA timestamp as a date
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @param string [$format='d/m/Y'] The format to generate
	 * @return string
	 */
	public static function getDate($timestamp = false, $format = 'd/m/Y') {
		if(!$timestamp) $timestamp = self::now();
		return date($format, strtotime($timestamp));
	}
	
	/**
	 * Format a NINA timestamp in a pretty way
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return string
	 */
	public static function getPrettyDate($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("D jS M 'y", strtotime($timestamp));
	}
	
	/**
	 * Format a NINA timestamp as a time
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @param string [$format='H:i:s'] The format to generate
	 * @return string
	 */
	public static function getTime($timestamp = false, $format = 'H:i:s') {
		if(!$timestamp) $timestamp = self::now();
		return date($format, strtotime($timestamp));
	}
	
	/**
	 * Format a NINA timestamp as a date/time
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return string
	 */
	public static function getDateTime($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("d/m/Y H:i:s", strtotime($timestamp));
	}
	
	/**
	 * Format a NINA timestamp as a pretty date/time
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return string
	 */
	public static function getPrettyDateTime($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("D jS M 'y g:ia", strtotime($timestamp));
	}
	
	/**
	 * Format a NINA timestamp as a MySQL date/time
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return string
	 */
	public static function getMysqlDateTime($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("Y-m-d H:i:s", strtotime($timestamp));
	}
	
	/**
	 * Get the day from a NINA Timestamp
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function getDay($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("d", strtotime($timestamp));
	}
	
	/**
	 * Get the month from a NINA Timestamp
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function getMonth($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("m", strtotime($timestamp));
	}
	
	/**
	 * Get the year from a NINA Timestamp
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function getYear($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("Y", strtotime($timestamp));
	}
	
	/**
	 * Get the hour from a NINA Timestamp
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function getHour($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("H", strtotime($timestamp));
	}
	
	/**
	 * Get the minutes from a NINA Timestamp
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function getMinute($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("i", strtotime($timestamp));
	}
	
	/**
	 * Get the seconds from a NINA Timestamp
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function getSecond($timestamp = false) {
		if(!$timestamp) $timestamp = self::now();
		return date("s", strtotime($timestamp));
	}
	
	/**
	 * Add minutes
	 * @param integer $minutes The minutes to add
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function addMinutes($minutes, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp + $minutes minutes"));
	}
	
	/**
	 * Subtract minutes
	 * @param integer $minutes The minutes to subtract
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function subtractMinutes($minutes, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp - $minutes minutes"));
	}
	
	/**
	 * Add hours
	 * @param integer $hours The hours to add
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function addHours($hours, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp + $hours hours"));
	}
	
	/**
	 * Subtract hours
	 * @param integer $hours The hours to subtract
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function subtractHours($hours, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp - $hours hours"));
	}
	
	/**
	 * Add days
	 * @param integer $days The days to add
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function addDays($days, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp + $days days"));
	}
	
	/**
	 * Subtract days
	 * @param integer $days The days to subtract
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function subtractDays($days, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp - $days days"));
	}
	
	/**
	 * Add weeks
	 * @param integer $weeks The weeks to add
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function addWeeks($weeks, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp + $weeks weeks"));
	}
	
	/**
	 * Subtract weeks
	 * @param integer $weeks The weeks to subtract
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function subtractWeeks($weeks, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp - $weeks weeks"));
	}
	
	/**
	 * Add months
	 * @param integer $months The months to add
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function addMonths($months, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp + $months months"));
	}
	
	/**
	 * Subtract months
	 * @param integer $months The months to subtract
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function subtractMonths($months, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp - $months months"));
	}
	
	/**
	 * Add years
	 * @param integer $years The years to add
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function addYears($years, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';

		return date("YmdHis", strtotime("$timestamp + $years years"));
	}
	
	/**
	 * Subtract years
	 * @param integer $years The years to subtract
	 * @param integer [$timestamp=false] The NINA timestamp
	 * @return integer
	 */
	public static function subtractYears($months, $timestamp = false) {
		if(!$timestamp) $timestamp = 'now';
		return date("YmdHis", strtotime("$timestamp - $years years"));
	}
}
